package com.example;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MajorDAO {
    public List<Major> getAllMajors() throws SQLException {
        List<Major> majors = new ArrayList<>();
        String sql = "SELECT * FROM majors ORDER BY name";
        
        try (Connection conn = DatabaseUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            
            while (rs.next()) {
                majors.add(new Major(
                    rs.getInt("id"),
                    rs.getString("name"),
                    rs.getInt("department_id"),
                    rs.getString("description"),
                    rs.getString("created_at")
                ));
            }
        }
        return majors;
    }

    public List<Major> getMajorsByDepartment(int departmentId) throws SQLException {
        List<Major> majors = new ArrayList<>();
        String sql = "SELECT * FROM majors WHERE department_id = ? ORDER BY name";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, departmentId);
            ResultSet rs = pstmt.executeQuery();
            
            while (rs.next()) {
                majors.add(new Major(
                    rs.getInt("id"),
                    rs.getString("name"),
                    rs.getInt("department_id"),
                    rs.getString("description"),
                    rs.getString("created_at")
                ));
            }
        }
        return majors;
    }

    public void addMajor(Major major) throws SQLException {
        String sql = "INSERT INTO majors (name, department_id, description) VALUES (?, ?, ?)";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, major.getName());
            pstmt.setInt(2, major.getDepartmentId());
            pstmt.setString(3, major.getDescription());
            pstmt.executeUpdate();
        }
    }

    public void updateMajor(Major major) throws SQLException {
        String sql = "UPDATE majors SET name = ?, department_id = ?, description = ? WHERE id = ?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, major.getName());
            pstmt.setInt(2, major.getDepartmentId());
            pstmt.setString(3, major.getDescription());
            pstmt.setInt(4, major.getId());
            pstmt.executeUpdate();
        }
    }

    public void deleteMajor(int id) throws SQLException {
        String sql = "DELETE FROM majors WHERE id = ?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, id);
            pstmt.executeUpdate();
        }
    }
} 